package com.lc.zpyh.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lc.zpyh.R;
import com.lc.zpyh.bean.TimeBean;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.ButterKnife;

public class ServiceTimeDialog  extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView tv_sure;

    private RecyclerView rv_time;
    List<TimeBean> dataarrtBeanList ;
    private ServiceTimeDialog.OnGoodsOrderListener onGoodsOrderListener;
    private BaseQuickAdapter<TimeBean, BaseViewHolder> shopAdapter;
    private String time;
    private String lijitime;

    public ServiceTimeDialog(Context context, List datalist) {
        super(context, R.style.dialog);
        this.context = context;
        this.dataarrtBeanList=datalist;


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置为居中
//        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        setContentView(R.layout.dialog_servicetime);
        WindowManager windowManager = ((Activity) context).getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失



        initView();


    }
    private void initView() {

        rv_time = findViewById(R.id.rv_time);
        tv_sure = findViewById(R.id.tv_sure);

        tv_sure.setOnClickListener(this);
        GridLayoutManager layoutManager = new GridLayoutManager(context,4);

        rv_time.setLayoutManager(layoutManager);
        dataarrtBeanList.get(0).setIsgrop(true);
        time = getDate(dataarrtBeanList.get(0).getTime());
        lijitime="立即送达";
        shopAdapter = new BaseQuickAdapter<TimeBean,
                BaseViewHolder>(R.layout.item_dialog_time, dataarrtBeanList) {

            @Override
            protected void convert(BaseViewHolder holder, TimeBean bean) {
                if (bean.isIsgrop()) {
                    holder.setBackgroundResource(R.id.ll_bg, R.mipmap.bg_true_s);

                }else {
                    holder.setBackgroundResource(R.id.ll_bg, R.drawable.bg_verification_code);
                }
                if (holder.getPosition()==0){
                    holder.setText(R.id.tv_time, "立即送达");
                }else {
                    holder.setText(R.id.tv_time, getDate(bean.getTime())+"");
                }

            }


        };
        shopAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                for (TimeBean bean :dataarrtBeanList){
                    bean.setIsgrop(false);
                }
                TimeBean bean = dataarrtBeanList.get(position);
                bean.setIsgrop(!bean.isIsgrop());
                time = getDate(dataarrtBeanList.get(position).getTime());
                if (position==0){
                    lijitime="立即送达";
                }else {
                    lijitime= getDate(dataarrtBeanList.get(position).getTime());
                }
                shopAdapter.notifyDataSetChanged();
            }
        });
        rv_time.setAdapter(shopAdapter);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_sure:

                if (onGoodsOrderListener != null) {
                    onGoodsOrderListener.onBuyNow(time,lijitime);
                    dismiss();
                }
                break;



            default:
                break;
        }
    }
    public interface OnGoodsOrderListener {
        void onDismiss( String time);


        void onBuyNow( String time,String lijitime);


    }
    private String getDate(long milliSeconds) {

        SimpleDateFormat formatter = new SimpleDateFormat("H:mm");
        // Create a calendar object that will convert the date and time value in
        // milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis( milliSeconds*1000L);
        return formatter.format(calendar.getTime());
    }
    public void setOnGoodsOrderListener(ServiceTimeDialog.OnGoodsOrderListener onGoodsOrderListener) {
        this.onGoodsOrderListener = onGoodsOrderListener;
    }
}
